Continue to implement get history
Implement getFavoriteItemsmethod
1 | /** |
Next, let’s try getFavoriteItemIds and getCategories
1 | /** |
Get back to ItemHistory.java
, update doGet
method.
1 | /** |
test with http://localhost:8080/Jupiter/history?user_id=1111
Recommendation System
Widely exists in industry and it is a popular interview question.
Facebook: news feed, friends, ‘Do you know this friend?’的例子
LinkedIn: jobs, companies, acquaintances, ‘Are you interested in this job?’的例子
Amazon: 电饭锅的例子
Google Ads: 两颗红豆的例子
Engineering Design
Given a user, fetch all the events (ids) this user has visited. (which table? which API?)
1
2//history: history_id, user_id, item_id, last_favor_time.
Set<String> itemIds = getFavoriteItemIds(userId);given all these events, what are the categories? (which table? which API?)
1
2//categories: item_id, category.
Set<String> categories = getCategories(itemId);given these categories, what are the events that belong to them (which table? which API?)
1 | //external API |
- filter events that this user has visited
Code Implementation
Add a new package src/algorithm
. Add GeoRecommendation.java
.
Idea of this algorithm: First get the current user’s favourite items, then get all categories of this user’s favorites, sort them by descending order. Then get these categories, search items again, sort this items with distance(Ascending order), and add those items to the result. Category > Distance.
1 | public List<Item> recommendItems(String userId, double lat, double lon) { |
Step 2, Go to src/rpc/RecommendItem.java
, implement doGet()
method.
1 | /** |
Step 3, De-duplicate items in recommendation results.
Since we’re using set to save results from our recommendation function, we need do the de-duplication based on item_id
. So go to Item.java
, add hashCode()
and equals()
.
Eclipse provides an easy way to add this two methods. Careful, only select itemId
when generating these methods.